package org.kalai.unifypay.util;

import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.Member;
import org.kalai.unifypay.chanpay.Chanpay;
import org.kalai.unifypay.huanpay.HuanPay;

public final class ProxyPayUtil {
	private static final Logger logger = Logger.getLogger(ProxyPayUtil.class);
	public static void proxyPay(String settNo, Member member, long cent, String bizcode, boolean isD0) throws SQLException{
		logger.info(String.format("开始%s代付：%s-%s", bizcode, member.getMemberName(), cent+"分"));
		Map<String, String> map = null;
		if (bizcode.startsWith("1") && Integer.valueOf(bizcode) < 1005) {//微信的
			map = HuanPay.proxyPay(settNo, Constant.HUANPAY_NOTIFY_URL, cent, member.getMemberMobile(), 
					member.getMemberName(), member.getMemberCertno(), member.getBankNo(), member.getBankName(), member.getAcctNo(), isD0, true);
		} else if (bizcode.startsWith("1") && Integer.valueOf(bizcode) < 1011) {//民生银行微信通道 中信银行公众号通道   使用畅捷代付
			map = Chanpay.pay(settNo, member.getBankName(), member.getAcctNo(), member.getMemberName(), cent);
		} else if (bizcode.startsWith("2")) {
			map = Chanpay.pay(settNo, member.getBankName(), member.getAcctNo(), member.getMemberName(), cent);
		} else if (bizcode.startsWith("3")) {
			map = HuanPay.proxyPay(settNo, Constant.HUANPAY_NOTIFY_URL, cent, member.getMemberMobile(), 
					member.getMemberName(), member.getMemberCertno(), member.getBankNo(), member.getBankName(), member.getAcctNo(), isD0, false);
		} else {
			logger.error("错误的业务编码："+bizcode);
		}
		DataDao.getInstance().updateSettlement(settNo, map.get("respCode"), map.get("respDesc"), map.get("respCode").equals("0000") || map.get("respCode").equals("P000") ? 4 : 5);
	}
}
